$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'news/news/list',
        datatype: "json",
        colModel: [			
			{ label: 'ID', name: 'id', index: 'id', width: 50, key: true },
			{ label: '标题', name: 'title', index: 'title', width: 80 }, 			
			{ label: '封面图', name: 'coverimage', index: 'coverimage', width: 80,formatter:function (d) {
                    if(d)
                    {
                        return '<img src="'+d+'" style="width: 28px;height: 28px;"/>';
                    }
                    else
                    {
                        return '无';
                    }
                } },
			{ label: '新闻类型', name: 'newstype', index: 'newstype', width: 80,formatter:function (d) {
                    if(d == 'Innovation')
                    {
                        return '创新';
                    }
                    else if(d == 'News')
                    {
                        return '新闻动态';
                    }
                    else
                    {
                        return '故事';
                    }
                } },
			{ label: '是否推荐', name: 'isrecommend', index: 'isrecommend', width: 80,formatter:function (d) {
                    if(d)
                    {
                        return '是';
                    }
                    else
                    {
                        return '否';
                    }
                } },
			{ label: '来源', name: 'source', index: 'source' },
			{ label: '备注', name: 'remark', index: 'remark' },
			//{ label: '内容', name: 'content', index: 'content', width: 80 },
			{ label: '发布时间', name: 'publishDate', index: 'publish_date', width: 80 }, 			
			{ label: '排序号', name: 'sortno', index: 'sortno', width: 80 }, 			
			{ label: '是否启用', name: 'isavailable', index: 'isavailable', width: 80,formatter:function (d) {
                    if(d)
                    {
                        return '是';
                    }
                    else
                    {
                        return '否';
                    }
                } },
			{ label: '创建时间', name: 'adddate', index: 'adddate', width: 120 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    new AjaxUpload('#upload', {
        action: baseURL + "uploadfile/fileupload/upload",
        name: 'file',
        autoSubmit:true,
        responseType:"json",
        onSubmit:function(file, extension){
            if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))){
                alert('只支持jpg、png、gif格式的图片！');
                return false;
            }
        },
        onComplete : function(file, r){
            if(r.code == 0){
                layer.msg(r.msg, {icon: 1});
                $('#coverimg').attr("src",r.url);
                $('#coverimage').val(r.url);
                vm.news.coverimage = r.url;
            }else{
                alert(r.msg);
            }
        }
    });

});

//ueditor编辑器
var ue = UE.getEditor('content');;

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		news: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.news = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)

		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.news.id == null ? "news/news/save" : "news/news/update";
                vm.news.content = encodeURI(ue.getContent());
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.news),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "news/news/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "news/news/info/"+id, function(r){
                vm.news = r.news;
                //赋值
                ue.setContent(vm.news.content)
                //取值
                var content = ue.getContent();
                //输出测试
                console.log(content);
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});